//异步串行钩子,每个任务依次执行完
//tapAsync(cb)
//tapPromise()
const { AsyncSeriesHook } = require('tapable');

//回调写法
const callback = () => {
	console.time('callback:success:end');
	const queue = new AsyncSeriesHook(['arg1', 'arg2']);

	queue.tapAsync('nodeTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:nodeTask', arg1, arg2);
			done();
		}, 1000);
	});
	queue.tapAsync('reactTask', (arg1, arg2, done) => {
		setTimeout(() => {
			console.log('callback:reactTask', arg1, arg2);
			done();
		}, 1000);
	});

	queue.callAsync('111', '222', () => {
		console.timeEnd('callback:success:end');
	});
};

callback();

//Promise写法
const promise = () => {
	console.time('promise:end');
	const queue = new AsyncSeriesHook(['arg1', 'arg2']);

	queue.tapPromise('nodeTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:nodeTask', arg1, arg2);
				resolve();
			}, 1000);
		});
	});
	queue.tapPromise('reactTask', (arg1, arg2) => {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				console.log('promise:reactTask', arg1, arg2);
				reject();
			}, 1000);
		});
	});

	queue
		.promise('111', '222')
		.then(() => {
			console.timeEnd('promise:success:end');
		})
		.catch(error => {
			console.timeEnd('promise:error:end');
		});
};

// promise();
